﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;
using System.IO.Compression;
namespace server
{
    class ProjectToUserDB
    {
        private UserData userdata = new UserData();
        public string Projectpath,Jobpath;
        private ArrayList members = new ArrayList();
        private XML_Job Job;
        private XML_Project Project;
        private ArrayList projectID = new ArrayList();
        private ArrayList projectName = new ArrayList();

        public ProjectToUserDB(string path1,string path2)
        {
            userdata.ReadDb();
            this.Projectpath = path1;
            this.Jobpath = path2;
            
        }
        /// <summary>
        /// 将Project.XML有用信息提取到Userdb中
        /// </summary>
        /// <returns>是否转换成功</returns>
        public bool Xml_ProjectToUser_DB()
        {
            try
            {
                Project = new XML_Project(Projectpath);
                projectID = Project.GetID();
                projectName = Project.GetName();

                UserData.Project proj = new UserData.Project();
                proj.ProjId = Convert.ToInt32(projectID[projectID.Count - 1]);
                proj.ProjName = projectName[projectID.Count - 1].ToString();
                userdata.Projects.Add(proj.ProjId, proj);

                userdata.WriteDb();
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }           
        }
        /// <summary>
        /// 将job.XML有用信息提取到Userdb中
        /// </summary>
        /// <returns>是否转换成功</returns>
        public bool Xml_JoinProject_DB(string name)
        {
            try
            {
                Job = new XML_Job(Jobpath);
                string projectname=Job.GetProject(name);
                string job = Job.GetJob(name);

                UserData.UserAssignProj assign = new UserData.UserAssignProj();
                foreach (KeyValuePair<string, UserData.User> user in this.userdata.Users)
                {
                    if (user.Value.UserName == name)
                    {
                        assign.ProjId = Convert.ToInt32(projectname);
                        assign.Job = job;
                        user.Value.AssignProj.Add(assign);
                    }
                }

                userdata.WriteDb();
                return true;
            }
            catch (System.Exception e)
            {
                return false;

            }
        }
        /// <summary>
        /// 删除一个用户信息，包括参与项目的工作信息
        /// </summary>
        /// <param name="name">用户名称</param>
        /// <param name="Projname">项目名称</param>
        /// <returns>是否操作成功</returns>
        public bool DeleteOneUser(string name,string Projname)
        {
            try
            {
                foreach (KeyValuePair<string, UserData.User> user in this.userdata.Users)
                {
                    if (user.Value.UserName == name)
                    {
                        foreach (UserData.UserAssignProj assign in user.Value.AssignProj)
                        {
                            UserData.Project proj;
                            userdata.Projects.TryGetValue(assign.ProjId, out proj);
                            if (proj.ProjName == Projname)
                            {
                                user.Value.AssignProj.Remove(assign);
                                break;
                            }
                        }
                    }
                }
                userdata.WriteDb();
                return true;
            }
            catch (System.Exception e)
            {
                return false;

            }
        }
        /// <summary>
        /// 删除一个项目信息
        /// </summary>
        /// <param name="Projname">项目名称</param>
        /// <returns>是否操作成功</returns>
        public bool DeleteOneProject(string Projname)
        {
            try
            {
                Project = new XML_Project(Projectpath);
                string ProjID = Project.GetID(Projname);
                userdata.Projects.Remove(Convert.ToInt32(ProjID));
                userdata.WriteDb();
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }
    }
}
